package top.lyc829.zzl_android.ui.index;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.MyApp;
import top.lyc829.zzl_android.logic.bean.BannerBean;
import top.lyc829.zzl_android.logic.bean.BaseBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Url;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class IndexViewModel extends ViewModel {

    private MutableLiveData<List<BannerBean>> bannerList;

    private MutableLiveData<List<BaseBean>> baseList;


    public IndexViewModel() {
        bannerList = new MutableLiveData<>();
        baseList = new MutableLiveData<>();
    }

    public MutableLiveData<List<BannerBean>> getBannerList() {
        return bannerList;
    }

    public void getBannersFromServer(String location) {
        HttpUtil httpUtil = HttpUtil.getInstance(MyApp.getContext());

        try {
            HashMap<String, String> body = new HashMap<>();
            body.put("location", location);
            httpUtil.get(Url.GET_BANNERS,body, new HttpUtil.MyCallback() {
                @Override
                public void success(Response res) throws IOException {
                    String resStr = res.body().string();
                    ResultBean<List<BannerBean>> resultBean = JsonUtilNew.fromJsonArray(resStr, BannerBean.class);
                    if (resultBean.getCode().equals(200)) {
                        bannerList.postValue(resultBean.getData());
                    } else {
                        failed(new IOException(resultBean.getMsg()));
                    }
                }
                @Override
                public void failed(IOException e) {
                    Utils.Sout(e.getMessage());
                }
            });
        }catch (Exception e) {
            Utils.SoutErr("getBannersFromServer: " + e.getMessage());
        }

    }


    public MutableLiveData<List<BaseBean>> getBaseList() {
        return baseList;
    }

    public void getBasesFromServer(int type, int page, String city) {
        HashMap<String, String> body = new HashMap<>();
        body.put("type", String.valueOf(type));
        body.put("page", String.valueOf(page));
        body.put("city", city);
        HttpUtil httpUtil = HttpUtil.getInstance(MyApp.getContext());

        try {
            httpUtil.get(Url.GET_INDEX_BASES_INFO, body, new HttpUtil.MyCallback() {
                @Override
                public void success(Response res) throws IOException {

                    ResultBean<List<BaseBean>> resultBean = JsonUtilNew.fromJsonArray(res.body().string(), BaseBean.class);
                    if (resultBean.getCode().equals(200)) {
                        baseList.postValue(resultBean.getData());
                    } else {
                        failed(new IOException(resultBean.getMsg()));
                    }

                }
                @Override
                public void failed(IOException e) {
                    Utils.Sout(e.getMessage());
                }
            });
        } catch (Exception e) {
            Utils.SoutErr("getBasesFromServer: " + e.getMessage());
        }


    }


}